package com.financing.wap.web.controller.tryApi;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dao.IUserDao;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.util.StringUtil;

/**
 * 可乐计划  产品分享专用
 * @author ios
 *
 */
@Controller("kljhController")
@RequestMapping("/api/try/kljh")
public class KljhController extends BaseTryController {
	
	
	@Autowired
	private IUserDao userDao;
	
	/**
	 * 可乐计划 添加分享信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/count")
	@ResponseBody
	public ResultMap count(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		
		log.info("begin in kljh count");
		
		Map<String,Object> param = new HashMap<String,Object>();
		String openId = request.getParameter("openId");
		String mobile = request.getParameter("mobile");
		String productId = request.getParameter("productId");
		
		
		//openId为空时直接抛出异常
		if(StringUtil.isNullOrEmptyStr(openId)||StringUtil.isNullOrEmptyStr(productId)){
			result.setStatus(STATUS.FAILE);
			result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
			result.setMessage("参数缺失");
			log.error("kljh count params lose");
			return result;
		}
		
		if(StringUtil.isNullOrEmptyStr(mobile)){
			mobile = "4008558085";
		}
		
		UserDTO user = new UserDTO();
		user.setLoginName(mobile);
		user = userDao.queryUserByMobile(user);
		
		if(user!=null){
			param.put("faId", user.getId());
		}
		param.put("openId", openId);
		param.put("mobile", mobile);
		param.put("productId", productId);
		
		Map res =   (Map) this.sqlMapClientTemplate.queryForObject("kljh.queryKljh",param);
		
		if(res != null && res.size()>0){
			param.put("shareCount", Integer.parseInt((String) res.get("shareCount"))+1);
			param.put("updateDate", new Date(System.currentTimeMillis()));
			param.put("updateBy", mobile);
			this.sqlMapClientTemplate.update("kljh.updateKljh",param);
		}else{
			param.put("shareCount", 1);
			param.put("createDate", new Date(System.currentTimeMillis()));
			param.put("createBy", mobile);
			param.put("updateDate", new Date(System.currentTimeMillis()));
			param.put("updateBy", mobile);
			this.sqlMapClientTemplate.insert("kljh.addKljh",param);
		}
		
		result.setStatus(STATUS.SUCCESS);
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		result.setAttribute("info", param);
		log.info("end in kljh count");
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public ResultMap query(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		try{
			log.info("begin in kljh query");
			Map<String,Object> param = new HashMap<String,Object>();
			String openId = request.getParameter("openId");
			String mobile = request.getParameter("mobile");
			String productId = request.getParameter("productId");
			
			
			//openId为空时直接抛出异常
			if(StringUtil.isNullOrEmptyStr(openId)||StringUtil.isNullOrEmptyStr(productId)){
				result.setStatus(STATUS.FAILE);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setMessage("参数缺失");
				log.info("kljh query params lose");
				return result;
			}
			
			param.put("openId", openId);
			param.put("mobile", mobile);
			param.put("productId", productId);
			
			@SuppressWarnings("deprecation")
			Map res =   (Map) this.sqlMapClientTemplate.queryForObject("kljh.queryKljh",param);
			log.info("查询结果："+res);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setAttribute("info", res);
			log.info("end in kljh query");
		}catch(Exception e){
			log.error("error inkljh query :",e);
			
		}
		
		return result;
	}
	
}
